﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class SearchFieldContributionOutput:ResultBase 
    {
        private string _ResultFilePath;
        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }

        private Dictionary<string,double> _FieldWeights;
        /// <summary></summary>
        public Dictionary<string,double> FieldWeights
        {
            get
            {
                return this._FieldWeights;
            }
            set
            {
                this._FieldWeights = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public SearchFieldContributionOutput():base(Guid.Empty, Guid.Empty)
        {
            this._ResultFilePath = string.Empty;
            this._FieldWeights=new Dictionary<string, double>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="resultFilePath"></param>
        /// <param name="fieldWeights"></param>
        public SearchFieldContributionOutput(Guid componentID, Guid jobTicketID,
            string resultFilePath, Dictionary<string,double> fieldWeights) 
            : base(componentID, jobTicketID)
        {
            this.ResultFilePath = resultFilePath;
            this._FieldWeights = fieldWeights;
        }

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            if(this.FieldWeights !=null && this.FieldWeights.Count>0)
            {
                XmlNode fieldWeightRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Fields");
                foreach(string fieldName in this.FieldWeights.Keys)
                {
                    XmlNode fieldNode = XmlDataUtil.AddElement(ref xDoc, fieldWeightRootNode, "Field");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "FieldName",fieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "Weight",this.FieldWeights[fieldName].ToString());
                }
            }

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                SearchFieldContributionOutput output = new SearchFieldContributionOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");
                output.FieldWeights=new Dictionary<string, double>();
                XmlNodeList fieldNodes = xNode.SelectNodes("Fields/Field");
                if(fieldNodes !=null && fieldNodes.Count>0)
                {
                    foreach(XmlNode fieldNode in fieldNodes)
                    {
                        string fieldName = XmlDataUtil.GetAttributeValue(fieldNode, "FieldName", "");
                        double weight = double.Parse(XmlDataUtil.GetAttributeValue(fieldNode, "Weight", "1.0"));
                        output.FieldWeights.Add(fieldName, weight);
                    }
                }
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
